package timing.ukulele.portal.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import timing.ukulele.data.portal.view.SysApplicationVO;
import timing.ukulele.persistence.service.BaseService;
import timing.ukulele.portal.convertor.ApplicationConverter;
import timing.ukulele.portal.mapper.SysApplicationMapper;
import timing.ukulele.portal.persistent.SysApplication;

import java.util.List;

@Service
public class SysApplicationService extends BaseService<SysApplicationMapper, SysApplication> {
    public IPage<SysApplicationVO> getPage(String code, String name, Boolean deleted, int current, int size) {
        Page<SysApplication> page = new Page<>(current, size);
        LambdaQueryChainWrapper<SysApplication> query = this.lambdaQuery();
        query.like(StringUtils.hasText(code), SysApplication::getCode, code);
        query.like(StringUtils.hasText(name), SysApplication::getName, name);
        query.eq(deleted != null, SysApplication::getDeleted, deleted);
        query.orderByDesc(SysApplication::getId);
        IPage<SysApplication> iPage = query.page(page);
        Page<SysApplicationVO> voPage = new Page<>(current, size);
        if (iPage != null && !CollectionUtils.isEmpty(iPage.getRecords())) {
            List<SysApplicationVO> voList = ApplicationConverter.INSTANCE.p2vList(iPage.getRecords());
            voPage.setRecords(voList);
            voPage.setTotal(iPage.getTotal());
        }
        return voPage;
    }
}
